package com.mrtan.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.mrtan.bean.Account;
import com.mrtan.bean.Permission;
import com.mrtan.bean.Role;
import com.mrtan.dto.UserInfoDto;
import com.mrtan.service.AccountService;
import com.mrtan.vo.LoginVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 登录
 *
 * @author 李聪
 * @version 2023/12/6
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    @Resource
    AccountService accountServlet;

    @RequestMapping("/login")
    public SaResult login(@Validated @RequestBody LoginVO loginVo) {
        if (loginVo.getUsername() == null || loginVo.getPassword() == null) {
            return SaResult.error("登陆失败:账号或密码不能为空");
        }
        System.out.println(loginVo);
        Account account = accountServlet.findByUsernameAndPassword(loginVo.getUsername(), loginVo.getPassword());
        if (account == null) {
            return SaResult.error("登陆失败:用户名或密码错误");
        }
        StpUtil.login(account.getId());
        account.setRoles(null);
        return SaResult.ok("登录成功").setData(StpUtil.getTokenValue());
    }

    //user/info?token=admin-token
    @RequestMapping("/info")
    public SaResult info(String token) {
        int loginIdByToken = Integer.parseInt((String) StpUtil.getLoginIdByToken(token));
        Account byId = accountServlet.findById(loginIdByToken);
        List<String> roleList = getRoleList(byId);
        List<String> permissionList = getPermissionList(byId);



        UserInfoDto userInfo = new UserInfoDto();
        userInfo.setRoles(roleList);
        userInfo.setName(byId.getUsername());
        userInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        userInfo.setId(byId.getId());
        byId.setRoles(null);
        return SaResult.ok("登录成功").setData(userInfo);
    }

    //user/info?token=admin-token
    @RequestMapping("/logout")
    public SaResult logout(String token) {
        StpUtil.logout();
        return SaResult.ok("注销成功");
    }

    public List<String> getRoleList(Account account) {
        //得到当前登录用户的角色列表
        Set<Role> roles = account.getRoles();
        Set<String> roleCodes = new HashSet<>();
        if (roles != null) {
            for (Role role : roles) {
                roleCodes.add(role.getCode());
            }
        }
        return new ArrayList<>(roleCodes);
    }

    public List<String> getPermissionList(Account account) {
        Set<Role> roles = account.getRoles();
        Set<String> permissions = new HashSet<>();
        if (roles != null) {
            for (Role role : roles) {
                Set<Permission> permissionSet = role.getPermissions();
                for (Permission permission : permissionSet) {
                    permissions.add(permission.getCode());
                }

                return new ArrayList<>(permissions);
            }
        }
        return Collections.emptyList();
    }
}
